/**
 * Created by lxl on 2017/2/15.
 */
layui.use([ 'form'], function () {
    var form = layui.form();
    $(document).ready(function () {
        var cols = [{
            title: '标签名',
            name: 'name',
            width: 150,
            align: 'center'
        }, {
            title: '标签描述',
            name: 'description',
            width: 150,
            align: 'center'
        }, {
            title: '标签类型',
            name: 'type',
            width: 150,
            align: 'center'
        }, {
            title: '必填',
            name: 'required',
            width: 150,
            align: 'center',
            renderer: function (val, item, rowIndex) {
                if(val){
                    return '是';
                }
                return '否';
            }
        }, {
            title: '操作',
            width: 150,
            align: 'center',
            renderer: function (val, item, rowIndex) {
                var btn = '<button class="layui-btn  edit" title="编辑"><i class="layui-icon" name="icon_edit">&#xe642;</i></button>'
                    + '<button class="layui-btn  delete" title="删除"><i class="layui-icon" name="icon_delete">&#xe640;</i></button>';
                return btn;
            }
        }];

        var url = '/tag/list';

        var mmg = $('#tag_list_table').mmGrid({
            checkCol: true,
            multiSelect: true,
            indexCol: true,
            indexColWidth: 55,
            height: 'auto',
            cols: cols,
            nowrap: true,
            fullWidthRows: true,
            params: {},
            url: url,
            method: 'post',
            root: 'list',
            autoLoad: true, //开启自动加载
            plugins: [$('#tag_list_paginator').mmPaginator()]
        }).on('loadSuccess', function (e, data) {
            autoIFrameHeight();
        }).on('cellSelected', function (e, item, rowIndex, colIndex) {
            if ($(e.target).is('.edit') || $(e.target).attr("name") == "icon_edit") {
                layer.open({
                    title: 'Edit',
                    type: 2,
                    shadeClose: true,
                    shade: 0.2,
                    area: ['60%', '80%'],
                    content: '/view/info/tag_add_edit.html#' + JSON.stringify(item).replace(/\"/g, "'"),  //iframe的url
                    end: function () {
                        mmg.load();
                    }
                });
            } else if ($(e.target).is('.delete') || $(e.target).attr("name") == "icon_delete") {
                layer.open({
                    content: "是否确认删除该标签？",
                    btn: ['否', '是'],
                    btn2: function (index, layero) {
                        var url = "/tag/remove";
                        var list = {tagDOs: [item]};
                        $.ajax({
                            type: "POST",
                            url: url,
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(list),
                            dataType: "json",
                            success: function (data) {
                                if (data.success) {
                                    layer.msg("删除标签成功");
                                    mmg.load();
                                } else {
                                    layer.msg("删除标签失败");
                                }
                            },
                            error: function (message) {
                                layer.msg("删除标签失败");
                            }
                        });
                    }
                })
            }
            else if ($(e.target).is('.preview') || $(e.target).attr("name") == "icon_preview") {
                layer.open({
                    type:1,
                    title:'preview',
                    content: createTag(item)
                })
            }
        });
        ;

        form.on('submit(add)', function () {
            layer.open({
                type: 2,
                title: 'Add',
                shadeClose: true,
                shade: 0.2,
                area: ['60%', '80%'],
                content: '/view/info/tag_add_edit.html',
                end: function () {
                    mmg.load();
                }
            });
            return false;
        });

        form.on('submit(delete)', function () {
            layer.open({
                content: "是否确认删除该标签？",
                btn: ['否', '是'],
                btn2: function (index, layero) {
                    if (mmg.selectedRows().length === 0) {
                        layer.msg("请选择标签");
                        return false;
                    }
                    var url = "/tag/remove";
                    var list = {tagDOs: mmg.selectedRows()};
                    $.ajax({
                        type: "POST",
                        url: url,
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(list),
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                layer.msg("删除标签成功");
                                mmg.load();
                            } else {
                                layer.msg("删除标签失败");
                            }
                        },
                        error: function (message) {
                            layer.msg("删除标签失败");
                        }
                    });
                }
            })
            return false;
        });

    });
});